package cn.itcast.web.task;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.system.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmailRemind {


    @Reference
    private ContractService contractService;

    @Autowired
    private UserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void remind() {
        List<Contract> contractList = contractService.findAll(new ContractExample());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        for (Contract contract : contractList) {
            Integer shipTime = Integer.valueOf(sdf.format(contract.getShipTime()));
            Integer nowTime = Integer.valueOf(sdf.format(new Date()));
            Integer emailTime = shipTime-nowTime;
            if (emailTime>=0 && emailTime<4){
              User user = userService.findById(contract.getCreateBy());
              Map<String, String> map = new HashMap<>();
              map.put("email",user.getEmail());
              map.put("title","船期提醒!");
              map.put("content",("亲爱的"+user.getUserName()+"用户,你的船期还有"+emailTime+"天就要到了哦!"));
              rabbitTemplate.convertAndSend("myExchange","msg.email", map);
              System.out.println("这会会发邮件");
          }
        }
    }


}

